package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.Brand;
import com.burgeon.backend.SHOP.entity.Classify;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.service.GoodsService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shop/good")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;




    @RequestMapping("typedata")
    public ResponseResult<List<Goods>> goodsByType(Integer start, String type, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.showGoodsByType(start, type, token, discount);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    @RequestMapping("all")
    public ResponseResult<List<Goods>> goodsAll(Integer start, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.getGoodsAll(token, discount, start);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    @RequestMapping("recommend")
    public ResponseResult<List<Goods>> showRecommendData(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.getRecommend(token, discount);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    //品牌搜索物品
    @RequestMapping("goodsbybrand")
    public ResponseResult<List<Goods>> showGoodsByBrand(Integer start, String id, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.findGoodsByBrandId(start, id, token, discount);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    //显示品牌分类
    @RequestMapping("brand")
    public ResponseResult<List<Brand>> showFindGoods(HttpSession session) {
        String token = session.getAttribute("token").toString();
        List<Brand> data = goodsService.showBrand(token);
        return new ResponseResult<List<Brand>>(SUCCESS, data);
    }

    //搜索商品
    @RequestMapping("find")
    public ResponseResult<List<Goods>> showFindGoods(Integer start, String name, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.findGoods(start, name, token, discount);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    @RequestMapping("goodsbyclassify")
    public ResponseResult<List<Goods>> showGoodsByClassify(Integer start, String classify, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        List<Goods> data = goodsService.getGoodsByClassify(start, token, classify, discount);
        return new ResponseResult<List<Goods>>(SUCCESS, data);
    }

    @RequestMapping("classify")
    public ResponseResult<List<Classify>> showClassify(HttpSession session) {
        if (null == session.getAttribute("token")) {
            return new ResponseResult<List<Classify>>(NOTLOG);
        }
        String token = session.getAttribute("token").toString();
        List<Classify> data = goodsService.getClassify(token);
        return new ResponseResult<List<Classify>>(SUCCESS, data);
    }

    @RequestMapping("show")
    public ResponseResult<Map> showGoods(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }

        Map data = goodsService.getGoodsData(token, discount);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("gooddataorder")
    public ResponseResult<Goods> findGoodsByIdByappOrderConfig(String id, HttpSession session) {

        String uid = "null";//未登录用户
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null != session.getAttribute("user_is_plus")) {
            isPlus = session.getAttribute("user_is_plus").toString();
        } else {
            isPlus = "N";
        }

        Goods data = goodsService.findGoodsByIdByappOrderConfig(id, uid, discount, isPlus);
        return new ResponseResult<Goods>(SUCCESS, data);
    }

    @RequestMapping("gooddata")
    public ResponseResult<Goods> GoodsData(String id, HttpSession session) {

        System.err.println("扫码id=" + id);
        String uid = "null";//未登录用户
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null != session.getAttribute("user_is_plus")) {
            isPlus = session.getAttribute("user_is_plus").toString();
        } else {
            isPlus = "N";
        }


        Goods data = goodsService.findGoodsById(id, uid, discount, isPlus);
        return new ResponseResult<Goods>(SUCCESS, data);
    }


    @RequestMapping("goodsDataBase")
    public ResponseResult<Goods> goodsDataBase(String id, HttpSession session) {
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        Goods data = goodsService.getGoodsDataByBase(id, discount);
        return new ResponseResult<Goods>(SUCCESS, data);
    }


    @RequestMapping("test")
    public ResponseResult<Goods> test() {
        String id="2179";
        String uid = "null";
        String token = "lfwd";
        String discount="1";

        Goods data = goodsService.findGoodsNumById(id, uid, token, discount);
        return new ResponseResult<Goods>(SUCCESS, data);
    }

    /**
     * 查询可配数量
     */
    @RequestMapping("gooddatanum")
    public ResponseResult<Goods> GoodDataNum(String id, HttpSession session) {
        String uid = "null";//未登录用户
        String token = session.getAttribute("token").toString();
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        } else {
            discount = "1";
        }
        Goods data = goodsService.findGoodsNumById(id, uid, token, discount);
        return new ResponseResult<Goods>(SUCCESS, data);
    }

    @RequestMapping("goodsbanners")
    public ResponseResult<List> getGoodsBanners(String id) {


        List data = goodsService.getGoodsBanners(id);
        return new ResponseResult<List>(SUCCESS, data);
    }


}
